package restless.core {
	
	import restless.core.gameObjs.RLEUpdateRenderObj;
	import restless.core.interfaces.IDispose;
	import restless.core.managers.RLEStatManager;
	
	public class RLEPool extends RLEUpdateRenderObj {
		
		protected var aObjs:Array = [];
		protected var vObjIDs:Vector.<String> = new Vector.<String>;
		protected var aPoolClasses:Array = [];
		protected var vPoolIDs:Vector.<String> = new Vector.<String>;
		protected var tempClass:Class;
		protected var nObjPool:int = 15;
		
		private var statM:RLEStatManager;
		private var i:int;
		
		public function RLEPool():void {
			setRE();
			statM = RE.statM;
			statM.addStat("OBJ_POOLED");
		}
		
		public function addClasses(classList:Vector.<Class>):void {
			var tempList:Vector.<Class> = classList;
			i = tempList.length - 1;
			for (i = 0; i > -1; i--) {
				tempClass = tempList[i];
				addClass(tempClass);
			}
		}
		
		public function addClass(someClass:Class):void {
			tempClass = someClass;
			var tempString:String = String(tempClass);
			vPoolIDs.push(tempString);
			aPoolClasses.push(tempClass);
			statM.addStat(String(tempClass));
		}
		
		public function removeClass(c:Class):void {
			i = aPoolClasses.indexOf(c);
			if (i >= 0) {
				aPoolClasses.splice(i, 1);
			}
		}
		
		public function addObject(obj:*):void {
			aObjs.unshift(obj);
			vObjIDs.unshift( String(obj) );
			checkObjPool();
		}
		
		public function getObj(id:String):* {
			i = vObjIDs.indexOf(id);
			return aObjs[i];
		}
		
		private function checkObjPool():void {
			i = aObjs.length;
			if (i != nObjPool) {
				nObjPool = i;
				aObjs.length = nObjPool;
				vObjIDs.length = nObjPool;
			}
		}
		
		public function setObjPoolAmount(num:int):void {
			nObjPool = num;
			checkObjPool();
		}
		
		override public function dispose():void {
			i = aPoolClasses.length - 1;
			for (i; i > -1; i--) {
				aPoolClasses[i].dispose();
			}
			aObjs = null;
			vObjIDs = null;
			vPoolIDs = null;
			aPoolClasses = null;
			tempClass = null;
			super.dispose();
		}
	}
}